<template>
  <div class="hot-categories">
    <div class="categories-grid">
      <div
        v-for="category in categories"
        :key="category.id"
        class="category-card"
        @click="goToCategory(category.id)"
      >
        <div class="category-image">
          <img v-img-fallback :src="category.image" :alt="category.name" />
          <div class="category-overlay">
            <div class="category-stats">
              <span class="product-count">{{ category.productCount }}+商品</span>
              <span class="hot-tag" v-if="category.isHot">🔥 热门</span>
            </div>
          </div>
        </div>
        
        <div class="category-info">
          <h3 class="category-name">{{ category.name }}</h3>
          <p class="category-desc">{{ category.description }}</p>
          
          <div class="category-tags">
            <span
              v-for="tag in category.tags"
              :key="tag"
              class="tag"
            >
              {{ tag }}
            </span>
          </div>
          
          <div class="category-action">
            <el-button type="primary" size="small">
              立即选购
              <el-icon class="ml-1"><ArrowRight /></el-icon>
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ArrowRight } from '@element-plus/icons-vue'
import { resolveImage } from '@/utils/request'
import { productApi } from '@/api/product'

const router = useRouter()

// 热门分类数据（后端联调）
const categories = ref([])

const fetchCategories = async () => {
  try {
    const res = await productApi.getCategories()
    const list = Array.isArray(res?.data) ? res.data : (res?.data?.list || [])
    const mapped = list.map(c => ({
      id: c.id,
      name: c.name,
      description: c.description || '',
      image: resolveImage(c.image),
      productCount: c.productCount || 0,
      isHot: !!c.isHot,
      tags: []
    }))
    categories.value = mapped
  } catch (e) {
    console.error('加载分类失败:', e)
  }
}

// 跳转到分类页面
const goToCategory = (categoryId) => {
  router.push({ name: 'Product', query: { category: categoryId } })
}

onMounted(() => {
  fetchCategories()
})
</script>

<style scoped>
.hot-categories {
  width: 100%;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: var(--spacing-lg);
}

.category-card {
  background-color: var(--bg-white);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-normal);
  cursor: pointer;
}

.category-card:hover {
  transform: translateY(-6px);
  box-shadow: var(--shadow-lg);
}

.category-image {
  position: relative;
  width: 100%;
  height: 200px;
  overflow: hidden;
}

.category-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform var(--transition-normal);
}

.category-card:hover .category-image img {
  transform: scale(1.1);
}

.category-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0%,
    rgba(0, 0, 0, 0.3) 70%,
    rgba(0, 0, 0, 0.7) 100%
  );
  display: flex;
  align-items: flex-end;
  padding: var(--spacing-lg);
}

.category-stats {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xs);
}

.product-count {
  color: var(--text-white);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
}

.hot-tag {
  color: var(--text-white);
  font-size: var(--font-size-xs);
  background-color: rgba(255, 255, 255, 0.2);
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--radius-sm);
  backdrop-filter: blur(10px);
  width: fit-content;
}

.category-info {
  padding: var(--spacing-lg);
}

.category-name {
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
  margin: 0 0 var(--spacing-sm) 0;
  line-height: var(--line-height-tight);
}

.category-desc {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  margin: 0 0 var(--spacing-md) 0;
  line-height: var(--line-height-normal);
}

.category-tags {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-xs);
  margin-bottom: var(--spacing-lg);
}

.tag {
  padding: var(--spacing-xs) var(--spacing-sm);
  background-color: var(--bg-gray-light);
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
  border-radius: var(--radius-sm);
  transition: all var(--transition-fast);
}

.tag:hover {
  background-color: var(--primary-color);
  color: var(--text-white);
}

.category-action {
  display: flex;
  justify-content: flex-end;
}

.ml-1 {
  margin-left: 4px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .categories-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: var(--spacing-md);
  }
}

@media (max-width: 768px) {
  .categories-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: var(--spacing-sm);
  }
  
  .category-image {
    height: 160px;
  }
  
  .category-info {
    padding: var(--spacing-md);
  }
  
  .category-name {
    font-size: var(--font-size-lg);
  }
}

@media (max-width: 480px) {
  .categories-grid {
    grid-template-columns: 1fr;
  }
  
  .category-image {
    height: 140px;
  }
  
  .category-info {
    padding: var(--spacing-sm);
  }
  
  .category-name {
    font-size: var(--font-size-base);
  }
  
  .category-desc {
    font-size: var(--font-size-xs);
  }
  
  .category-overlay {
    padding: var(--spacing-md);
  }
}
</style>